﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class OrderDetails : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection();
    SqlCommand cmd;
    SqlDataAdapter da;
    DataTable dt;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        con.ConnectionString = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        if (!IsPostBack)
        {
            ViewState["orderid"] = Request.QueryString["ID"];
            ViewState["oldproductid"] = null;
            LoadData();
            LoadDllProduct();
        }
    }

    private void LoadData()
    {
        try
        {
            con.Open();
            cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "getOrderDetails";

            SqlParameter param = new SqlParameter("@orderid", SqlDbType.Int);
            param.Value = int.Parse(ViewState["orderid"].ToString());
            cmd.Parameters.Add(param);

            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds);

            gvOrderDetails.AutoGenerateSelectButton = true;
            gvOrderDetails.AllowPaging = true;
            gvOrderDetails.PageSize = 10;

            gvOrderDetails.DataSource = ds.Tables[0];
            gvOrderDetails.DataBind();

            txtOrderID.Text = ViewState["orderid"].ToString();

        }
        catch (System.Exception ex)
        {
            Response.Write("<script>alert('" + "Load Fail!" + "')</script>");
        }
        finally
        {
            con.Close();
        }

    }

    private void LoadDllProduct()
    {
        try
        {
            SqlDataAdapter da = new SqlDataAdapter("SELECT productid, productname FROM Production.Products", con);
            DataTable dt = new DataTable("Products");
            da.Fill(dt);

            ddlProduct.DataSource = dt;
            ddlProduct.DataTextField = "productname";
            ddlProduct.DataValueField = "productid";
            ddlProduct.DataBind();
        }
        catch (System.Exception ex)
        {

        }
    }

    protected void gvOrderDetails_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            GridViewRow r = gvOrderDetails.SelectedRow;
            txtOrderID.Text = Server.HtmlDecode(r.Cells[1].Text);
            ddlProduct.SelectedValue = Server.HtmlDecode(r.Cells[2].Text);
            txtUnitPrice.Text = Server.HtmlDecode(r.Cells[3].Text);
            txtQuantity.Text = Server.HtmlDecode(r.Cells[4].Text);
            txtDiscount.Text = Server.HtmlDecode(r.Cells[5].Text);
            ViewState["oldproductid"] = ddlProduct.SelectedValue;

            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            btnAdd.Enabled = false;
        }
        catch (System.Exception ex)
        {

        }
    }

    private void AddData()
    {
        try
        {
            cmd = new SqlCommand();
            cmd.Connection = con;
            con.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "InsertOrderDetails";

            SqlParameter param;
            param = new SqlParameter("@orderid", SqlDbType.Int);
            param.Value = int.Parse(ViewState["orderid"].ToString());
            cmd.Parameters.Add(param);

            param = new SqlParameter("@productid", SqlDbType.Int);
            param.Value = ddlProduct.SelectedValue;
            cmd.Parameters.Add(param);

            param = new SqlParameter("@unitprice", SqlDbType.Money);
            param.Value = txtUnitPrice.Text;
            cmd.Parameters.Add(param);

            param = new SqlParameter("@qty", SqlDbType.SmallInt);
            param.Value = short.Parse(txtQuantity.Text);
            cmd.Parameters.Add(param);

            param = new SqlParameter("@discount", SqlDbType.Decimal);
            param.Value = decimal.Parse(txtDiscount.Text);
            cmd.Parameters.Add(param);

            cmd.ExecuteNonQuery();

        }
        catch (SqlException ex)
        {
            Response.Write("<script>alert('" + "Duplicated product!" + "')</script>");
        }
        catch (System.Exception ex)
        {
            Response.Write("<script>alert('" + "Cannot add data!" + "')</script>");
        }
        finally
        {
            con.Close();
            ClearData();
        }
    }

    private void UpdateData()
    {
        try
        {
            cmd = new SqlCommand();
            cmd.Connection = con;
            con.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "UpdateOrderDetails";

            SqlParameter param;
            param = new SqlParameter("@orderid", SqlDbType.Int);
            param.Value = int.Parse(ViewState["orderid"].ToString());
            cmd.Parameters.Add(param);

            param = new SqlParameter("@oldproductid", SqlDbType.Int);
            param.Value = int.Parse(ViewState["oldproductid"].ToString());
            cmd.Parameters.Add(param);

            param = new SqlParameter("@newproductid", SqlDbType.Int);
            param.Value = int.Parse(ddlProduct.SelectedValue);
            cmd.Parameters.Add(param);

            param = new SqlParameter("@unitprice", SqlDbType.Money);
            param.Value = txtUnitPrice.Text;
            cmd.Parameters.Add(param);

            param = new SqlParameter("@qty", SqlDbType.SmallInt);
            param.Value = short.Parse(txtQuantity.Text);
            cmd.Parameters.Add(param);

            param = new SqlParameter("@discount", SqlDbType.Decimal);
            param.Value = decimal.Parse(txtDiscount.Text);
            cmd.Parameters.Add(param);

            cmd.ExecuteNonQuery();

        }
        catch (SqlException ex)
        {
            Response.Write("<script>alert('" + "Duplicated product!" + "')</script>");
        }
        catch (System.Exception ex)
        {
            Response.Write("<script>alert('" + "Cannot update data!" + "')</script>");
        }
        finally
        {
            con.Close();
            ClearData();
        }
    }

    private void DeleteData()
    {
        try
        {
            cmd = new SqlCommand();
            cmd.Connection = con;
            con.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "DeleteOrderDetails";

            SqlParameter param = new SqlParameter("@productid", SqlDbType.Int);
            param.Value = ddlProduct.SelectedValue;
            cmd.Parameters.Add(param);

            cmd.ExecuteNonQuery();
        }
        catch (System.Exception ex)
        {
            Response.Write("<script>alert('" + "Cannot delete data!" + "')</script>");
        }
        finally
        {
            con.Close();
            ClearData();
        }

    }

    private void ClearData()
    {
        ddlProduct.SelectedIndex = 0;
        txtUnitPrice.Text = "";
        txtQuantity.Text = "";
        txtDiscount.Text = "";
        LoadData();
        btnNew.Enabled = true;
        btnAdd.Enabled = true;
        btnUpdate.Enabled = false;
        btnDelete.Enabled = false;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        ClearData();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        AddData();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        UpdateData();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        DeleteData();
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Orders.aspx");
    }
}
